// pages/search/search.js
import {
  request
} from '../../utils/http'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hotSearchList: [], //热门搜索
    oldSeachList: wx.getStorageSync('oldSeachList')||[],
    content: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('1111',wx.getStorageSync('oldSeachList'))
    //热门搜索
    request({
      url: "/search/hotSearchByShopId",
      data: {
        number: 5,
        shopId: 12,
        sort: 1,
      }
    }).then(res => {
      this.setData({
        hotSearchList: res,
      });
    })

    this.getHistryList()
  },
  //跳转搜索
  toSearchProdPage:function (e) {
    console.log('----', e.detail.value)
    if (e.detail.value) { //如果当前内容存在
      //获取本地储存中的
       let oldSeachList = wx.getStorageSync('oldSeachList') || [];
       console.log(oldSeachList)
       oldSeachList = oldSeachList.filter(item => item !== e.detail.value);
       oldSeachList.unshift(e.detail.value);
       wx.setStorageSync('oldSeachList', oldSeachList);

      //跳转列表页面
      wx.navigateTo({
        url: '/pages/searchDetail/searchDetail?values=' + e.detail.value,
      })
     }

  },
  //获取输入内容
  getSearchContent: function (e) {
    console.log(e.detail)
    this.setData({
      content: e.detail.content
    })
  },

  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getHistryList()
  },
  //获取历史搜索
  getHistryList:function(){
    let oldSeachList=wx.getStorageSync('oldSeachList');
    this.setData({
      oldSeachList:oldSeachList
    })
  },
  //删除历史记录
  clearSearch:function(e){
    wx.removeStorageSync('oldSeachList');
    this.getHistryList();
  },

  //跳转搜索列表页面
  openSearch:function(e){
    let values = e.currentTarget.dataset.name;
    this.setData({
      content: values
    });
    wx.navigateTo({
      url: '/pages/searchDetail/searchDetail?values=' + values,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})